<?php namespace App\Repositories\Eloquent\Money;

use App\Models\MoneyFlow;
use App\Models\User;
use App\Repositories\Money\MoneyRepositoryInterface;

class MoneyRepository implements MoneyRepositoryInterface
{
    /**
     * 记录用户消费信息账单
     *
     * @param        $user_id
     * @param        $order_id
     * @param        $money
     * @param string $remark
     *
     * @return bool
     */
    public function storeUserPayOrder($user_id, $order_id, $money, $remark = '')
    {
        return $this->store($user_id, MoneyFlow::TYPE_USER_PAY_ORDER, $order_id, $money * (-1), $remark);
    }

    /**
     * 记录发布信息收入账单
     *
     * @param        $user_id
     * @param        $order_id
     * @param        $money
     * @param string $remark
     *
     * @return bool
     */
    public function storeFromPublish($user_id, $order_id, $money, $remark = '')
    {
        return $this->store($user_id, MoneyFlow::TYPE_FROM_PUBLISH, $order_id, $money, $remark);
    }

    /**
     * 提现记录
     *
     * @param $user_id
     * @param $withdraw_id
     * @param $money
     * @param $remark
     *
     * @return MoneyFlow
     */
    public function storeWithdraw($user_id, $withdraw_id, $money, $remark = '')
    {
        return $this->store($user_id, MoneyFlow::TYPE_WITHDRAW, $withdraw_id, $money * (-1), $remark);
    }

    /**
     * 邀请发布者
     *
     * @param $user_id
     * @param $withdraw_id
     * @param $money
     * @param $remark
     *
     * @return MoneyFlow
     */
    public function storeInvitePublisher($user_id, $order_id, $money, $remark = '')
    {
        return $this->store($user_id, MoneyFlow::TYPE_INVITE_PUBLISHER, $order_id, $money, $remark);
    }

    /**
     * 邀请用户
     *
     * @param $user_id
     * @param $withdraw_id
     * @param $money
     * @param $remark
     *
     * @return MoneyFlow
     */
    public function storeInviteUser($user_id, $order_id, $money, $remark = '')
    {
        return $this->store($user_id, MoneyFlow::TYPE_INVITE_USER, $order_id, $money, $remark);
    }

    /**
     * 平台获取发布信息订单佣金
     *
     * @param        $order_id
     * @param        $money
     * @param string $remark
     *
     * @return MoneyFlow
     */
    public function storePlatformPublishCommission($order_id, $money, $remark = '')
    {
        return $this->storePlatform(MoneyFlow::TYPE_PLATFORM_PUBLISH_COMMISSION, $order_id, $money, $remark);
    }

    /**
     * 平台给邀请用户的人发放佣金
     *
     * @param        $order_id
     * @param        $money
     * @param string $remark
     *
     * @return MoneyFlow
     */
    public function storePlatformInviteUserCommission($order_id, $money, $remark = '')
    {
        return $this->storePlatform(MoneyFlow::TYPE_PLATFORM_INVITE_USER_COMMISSION, $order_id, $money, $remark);
    }

    /**
     * 平台给邀请发布者的人发放佣金
     *
     * @param        $order_id
     * @param        $money
     * @param string $remark
     *
     * @return MoneyFlow
     */
    public function storePlatformInvitePublisherCommission($order_id, $money, $remark = '')
    {
        return $this->storePlatform(MoneyFlow::TYPE_PLATFORM_INVITE_PUBLISHER_COMMISSION, $order_id, $money, $remark);
    }

    /**
     * 新增平台流水记录
     *
     * @param        $order_id
     * @param        $money
     * @param string $remark
     *
     * @return MoneyFlow
     */
    public function storePlatform($type, $order_id, $money, $remark = '')
    {
        return $this->store(User::PLATFORM_UID, $type, $order_id, $money, $remark);
    }

    /**
     * 存储流水记录
     *
     * @param        $user_id
     * @param        $object_type
     * @param        $object_id
     * @param        $money
     * @param string $remark
     *
     * @return MoneyFlow
     */
    public function store($user_id, $object_type, $object_id, $money, $remark = '')
    {
        $model = new MoneyFlow();
        $model::unguard();
        $model->fill(compact('user_id', 'object_type', 'object_id', 'money', 'remark'));
        $model->is_valid = MoneyFlow::VALID;
        $model->save();

        return $model;
    }
}
